# -*- coding = utf-8 -*-
# @Time : 2024/6/4 01:34
# @Author : 0505200213石浪
# @File : socketio_events.py
# @Software : PyCharm

# socketio_events.py

from flask_socketio import join_room, leave_room, send
from flask import request
from extensions import socketio

private_rooms = {}
no_read_message = {}


@socketio.on('join')
def join(data):
    # 如果字段不存在的语句
    if data['room'] in private_rooms:
        print("已存在该房间", data['room'])
        join_room(data['room'])
        private_rooms[data['room']]['num'] += 1
        send({'msg': data['username'] + " 加入了聊天室."}, room=data['room'])
    elif data['receive'] not in private_rooms:
        print("不在一个房，接受字段：", data['receive'])
        private_rooms[data['room']] = {'id': request.sid, 'receive': data['receive'], 'num': 1}
        print(private_rooms)
        join_room(data['room'])
        send({'msg': data['username'] + " 加入了聊天室."}, room=data['room'])
    # 字段存在时的语句1
    else:
        print("在一块儿", data['receive'])
        data['room'] = data['receive']
        private_rooms[data['room']]['num'] += 1
        print(private_rooms)
        join_room(data['room'])
        send({'msg': data['username'] + " 加入了聊天室."}, room=data['room'])
    print('房间：', private_rooms)


@socketio.on('text')
def text(data):
    print('消息',data)
    if data['receive'] not in private_rooms:
        if private_rooms[data['room']]['num'] == 1:  # 一个人在房里
            if data['friend'] in no_read_message:
                # 如果已经有了键值对，则添加值的内容
                no_read_message[data['friend']].add(data['username'] + ": " + data['msg'])
            else:
                # 如果没有该键值对，则创建新的键值对，并添加值
                no_read_message[data['friend']] = {data['username'] + ": " + data['msg']}
        send({'msg': data['username'] + ": " + data['msg']}, room=data['room'])
    else:
        if private_rooms[data['receive']]['num'] == 1:  # 一个人在房里
            if data['username'] in no_read_message:
                # 如果已经有了键值对，则添加值的内容
                no_read_message[data['friend']].add(data['username'] + ": " + data['msg'])
            else:
                # 如果没有该键值对，则创建新的键值对，并添加值
                no_read_message[data['friend']] = {data['username'] + ": " + data['msg']}
        send({'msg': data['username'] + ": " + data['msg']}, room=data['receive'])
    print('留言', no_read_message)


# if no_read_message['']


@socketio.on('left')
def left(data):
    if data['receive'] not in private_rooms:
        leave_room(data['room'])
        private_rooms[data['room']]['num'] -= 1
        if private_rooms[data['room']]['num'] == 0:
            del private_rooms[data['room']]
        print(private_rooms, "离开了房间：", data['room'])
        send({'msg': data['username'] + " 离开了聊天室."}, room=data['room'])
    else:
        leave_room(data['receive'])
        private_rooms[data['receive']]['num'] -= 1
        if private_rooms[data['receive']]['num'] == 0:
            del private_rooms[data['receive']]
        print(private_rooms, "离开了房间：", data['receive'])
        send({'msg': data['username'] + " 离开了聊天室."}, room=data['receive'])
